import { Service, SpringDao, Transactional } from 'j-spring-jpa';
import { Autowired } from 'j-spring';
import { UserEntity } from '@entity/userEntity';

@Service()
export class UserService {
  @Autowired()
  private springDao: SpringDao;

  @Transactional()
  public async doTestJpa() {
    const u = new UserEntity().of({
      userId: '111',
      userName: '2222',
    });
    await this.springDao.save(u);

    //search 1
    const userList = await this.springDao.query<UserEntity>(
      'select * from live_user where userId=?',
      ['111']
    );

    //search 2
    const userList2 = await this.springDao.queryEntity({
      clazz: UserEntity,
      where: {
        userId: '111',
      },
      pagin: {
        curPage: 1,
        pageSize: 20,
      },
      order: {
        userId: 'ASC',
      },
    });

    console.log({ userList, userList2 });

    //update1
    u.userName = 'newName';
    await this.springDao.update(u);

    //update2
    await this.springDao.updateProperty(
      UserEntity,
      { userName: 'newName' },
      { userId: '111' }
    );

    //update3
    await this.springDao.query(
      `update live_user set  userName=? where userId=?`,
      ['newName', '111']
    );

    return 'jpa test pass!';
  }
}
